package util

import (
	"fmt"
	"strconv"
	"strings"
)

// DefaultString make defaultString when string equal empty
func DefaultString(s string, ds string) string {
	if s == "" {
		return ds
	}
	return s
}

// AddSuffixWithParentheses  => "s(suffix)" or "s" when suffix is empty
func AddSuffixWithParentheses(s string, suffix string) string {
	if suffix == "" {
		return s
	}
	return fmt.Sprintf("%s(%s)", s, suffix)
}

func RightString(s string, n int) string {
	idx := len(s) - n
	if idx < 0 {
		idx = 0
	}
	return s[idx:]
}

func IsEmpty(s string) bool {
	return s == ""
}

func StringToInt64(s string) (i int64) {
	i, _ = strconv.ParseInt(s, 10, 64)
	return
}

func StringToInt32(s string) int32 {
	i, _ := strconv.ParseInt(s, 10, 32)
	return int32(i)
}

func StringFromInt64(i int64) (s string) {
	s = strconv.FormatInt(i, 10)
	return
}

func BeginWith(s, prefix string) bool {
	return strings.HasPrefix(s, prefix)
}

func EndWith(s, suffix string) bool {
	return strings.HasSuffix(s, suffix)
}

func Contains(s, suffix string) bool {
	return strings.Contains(s, suffix)
}

func SplitString(s, sep string) []string {
	return strings.Split(s, sep)
}
